//
//  AddSimpleViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/10.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "AddSimpleViewController.h"
#import "SimpleInfo.h"
#import "SimpleDynamicCellConstraintFactory.h"
#import "ChoiceEntAddressBookContactViewController.h"
#import "HeadNameViewPanel.h"
#import "ChoiceRecordViewController.h"
#import "KZVideoConfig.h"
#import "ChoiceVoiceViewController.h"

@interface AddSimpleViewController ()<HeadNameViewPanelDelegate>
{
    NSArray *_singleType;
    
    HeadNameViewPanel *_headNameViewPanel;
    
    NSMutableArray *_selectedMember;
    
    NSString *_singleSwitchValue;
    
    KZVideoModel *_model;
    
    NSString *_voicePath;
}
@end

@implementation AddSimpleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"新增";
    
    _vclType = DynamicColumnFieldViewTypeAdd;
    _singleType = [SimpleInfo totalSingleType];
    _selectedMember = [NSMutableArray new];
    _headNameViewPanel = [[HeadNameViewPanel alloc]initWithFrame:CGRectMake(0, 0, [UIDevice screenWidth], 50) delegate:self];
    _headNameViewPanel.left = 13;
    _singleSwitchValue = @"";
    
    [super initViewDidLoad];
    
    _transformMap = [SimpleInfo JSONObjectProperty:nil];
    
    _constraint = [SimpleDynamicCellConstraintFactory factoryForModify:nil];
    
    [super reloadDyanmicField];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)clickedRightBarButton:(id)sender
{
    [UIViewController closeKeyBoard];
    
    if([super verifyDynamicField:_constraint] == YES)
    {
        SimpleInfo *modelInfo = [SimpleInfo emptySimpleInfo];
        
        XMSQueueDictionary *qdSet = _transformMap;
        
        [super extractDynamicFieldValue:_constraint transformMap:qdSet packObj:modelInfo specialExtract:^BOOL(FieldDescript *fixField)
         {
             return NO;
         }];
        
        [XUIAlertView alertViewWithTitle:@"提示" message:@"保存成功" cancelButtonTitle:nil otherButtonTitles:@[@"确定"] clickBlock:^(NSInteger nClickindex)
         {
             [XUINavigationController pop];
         }];
    }
}

- (NSInteger)pickerDefaultRowInFieldDescript:(FieldDescript*)fieldDescript
{
    if([fieldDescript.fieldName isEqualToString:@"singleText"])
        return ((SimpleInfo*)_packObj).singleTextType.intValue - 1;
    return -1;
}

- (NSInteger)pickerNumberOfRowInFieldDescript:(FieldDescript*)fieldDescript
{
    if([fieldDescript.fieldName isEqualToString:@"singleText"])
        return _singleType.count;
    return -1;
}

- (NSString*)pickerTitleForRow:(NSInteger)row inFieldDescript:(FieldDescript*)fieldDescript
{
    if([fieldDescript.fieldName isEqualToString:@"singleText"])
    {
        return _singleType[row];
    }
    return nil;
}

- (NSString*)pickerSelectedTitleAtIndex:(NSInteger)nIndex inFieldDescript:(FieldDescript*)fieldDescript
{
    if([fieldDescript.fieldName isEqualToString:@"singleText"])
    {
        ((SimpleInfo*)_packObj).singleTextType.intValue = nIndex + 1;
        
        return _singleType[nIndex];
    }
    return nil;
}

#pragma mark --
#pragma mark -- override

-(void)didSelectedSwitchSelectGroupTableCell:(FieldDescript *)fieldDescript
{
    if([fieldDescript.fieldName isEqualToString:@"singleSwitchText"])
    {
        XUIViewControllerSwitchParam * p =[XUIViewControllerSwitchParam new];
        
        NSMutableArray *selectMember = [NSMutableArray new];
        [selectMember addObjectsFromArray:_selectedMember];
        
        NSMutableArray *ar = [[selectMember fetchArrayObjectPorpertyValueWithPorperty:@"userNumber"] mutableCopy];
        [ar addObject:[MyInfo myInfo].userNumber];
        
        p.param[ChoiceEntAddressBookContactViewControllerFilterChoiceContactParamkey] = ar;
        
        [XUINavigationController pushViewControllerClass:[ChoiceEntAddressBookContactViewController class] withSwitchParam:p];
    }
}

#pragma mark --
#pragma mark -- recv last viewController param

-(void)recvPopViewControllerSwitchParam:(XUIViewControllerSwitchParam *)switchParam
{
    if(switchParam.param[ChoiceEntAddressBookContactViewControllerChoiceContactParamkey] != nil)
    {
        [_selectedMember addObjectsFromArray:switchParam.param[ChoiceEntAddressBookContactViewControllerChoiceContactParamkey]];
        
        _singleSwitchValue = [NSString megerStringWithTag:_selectedMember value:^NSString *(id obj){
            EntAddressBookContact *c = obj;
            return c.userName;
        } tag:@","];
        
        SwitchSelectGroupTableViewCell *cell = (SwitchSelectGroupTableViewCell*)[_core expandDynamicCellWithTag:@"singleSwitchText"];
        cell.text = _singleSwitchValue;
        cell.showClearButton = YES;
    }
    else if(switchParam.param[ChoiceRecordViewControllerChoiceRecordParamKey] != nil)
    {
        _model = switchParam.param[ChoiceRecordViewControllerChoiceRecordParamKey];
        
        NSLog(@"%@",_model.videoAbsolutePath);
        
        MixedPanelGroupTableViewCell *cell = (MixedPanelGroupTableViewCell*)[_core expandDynamicCellWithTag:@"mixedPanelText"];
        
        UIImage *img = [UIImage imageNamed:@"img_video_default"];
        [cell addImage:img withTag:_model.videoAbsolutePath];
    }
    else if(switchParam.param[ChoiceVoiceViewControllerChoiceVoiceParamKey] != nil)
    {
        _voicePath = switchParam.param[ChoiceVoiceViewControllerChoiceVoiceParamKey];
        
        NSLog(@"%@",_voicePath);
        
        MixedPanelGroupTableViewCell *cell = (MixedPanelGroupTableViewCell*)[_core expandDynamicCellWithTag:@"mixedPanelText"];
        
        UIImage *img = [UIImage imageNamed:@"img_voice_default"];
        [cell addImage:img withTag:_voicePath];
    }
    else
        [super recvPopViewControllerSwitchParam:switchParam];
}

#pragma mark --
#pragma mark -- ClearButton
-(void)dynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore didClickedClearButtonOfDropDownGroupTableViewCell:(DropDownGroupTableViewCell *)cell
{
    if([cell.szTag isEqualToString:@"singleSwitchText"])
    {
        [_selectedMember removeAllObjects];
        _singleSwitchValue = @"";
        
        cell.text = _singleSwitchValue;
        cell.showClearButton = NO;
    }
}

#pragma mark --
#pragma mark -- headNameViewPanel delegate

-(void)headNameViewPanel:(HeadNameViewPanel *)panel resizeNewHeight:(CGFloat)height
{
    [_core reloadExpandDynamicCellsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:_constraint.count - 1]]];
}

-(void)headNameViewPanel:(HeadNameViewPanel *)panel clickAddBtn:(id)sender
{
    [UIViewController closeKeyBoard];
    
    XUIViewControllerSwitchParam * p = [XUIViewControllerSwitchParam new];
    
    NSMutableArray *selectMember = [NSMutableArray new];
    [selectMember addObjectsFromArray:_selectedMember];
    
    NSMutableArray *ar = [[selectMember fetchArrayObjectPorpertyValueWithPorperty:@"userNumber"] mutableCopy];
    
    [ar addObject:[MyInfo myInfo].userNumber];
    
    p.param[ChoiceEntAddressBookContactViewControllerFilterChoiceContactParamkey] = ar;
    
    [XUINavigationController pushViewControllerClass:[ChoiceEntAddressBookContactViewController class] withSwitchParam:p];
}

-(void)headNameViewPanel:(HeadNameViewPanel *)panel clickDeleteMaskBtn:(NSString *)szTag
{
    [panel removeHeadViewWithTag:szTag];
    
    for(EntAddressBookContact *c in _selectedMember)
    {
        if([[c.userNumber toNSString] isEqualToString:szTag])
        {
            [_selectedMember removeObject:c];
            break;
        }
    }
}

-(void)headNameViewPanel:(HeadNameViewPanel *)panel clickNormalStateBtn:(NSString*)szTag
{
    
}

@end
